package com.yh.servlet; /**
 * @Auther: Liang
 * @Date: 2024/5/16 - 05 - 16 - 11:13
 * @Description: ${PACKAGE_NAME}
 * @version: 1.0
 */

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "MyServlet2", value = "/MyServlet2")
public class MyServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //全局作用域
        ServletContext servletContext = request.getServletContext();

       //使用工具类，想其里面传入全局作用域类，它会自动取出spring容器对象
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        Object user = webApplicationContext.getBean("user");
        System.out.println(user);

    }

}
